var UEdit;
$(function () {
    layui.use('layer', function(){
        var layer = layui.layer;
    });
    //判断是否为阅读题子题修改
    if(sessionStorage.getItem("sonTitleModify")!=null){
        sessionStorage.removeItem("sonTitleModify");
        $("#questionType li[data-value='5']").css("display","none");
    }
    //文件上传
    //$("#file1").on("change", function () {
    //    if ($(this).val()) {
    //        $(this).parent().next()[0].innerHTML = "正在上传中......";
    //        filenameMd5(SystemConfig.fileUploadUrl+ SystemConfig.appKey, "file1");
    //    } else {
    //        $(this).parent().next()[0].innerHTML = "mp3/wav格式";
    //        $(this).next().val("");
    //    }
    //});
    $("#file2").on("change",function(){
        if ($(this).val()) {
            $(this).parent().parent().next()[0].innerHTML =  "正在上传中......";
            var fileName=$(this)[0].files[0].name;
            var inputDom=$(this);
            uploadFile(SystemConfig.fileUploadUrl+ SystemConfig.appKey + "/", $(this)[0].files[0], function (result) {
                var result = JSON.parse(result);
                if (result.status == 1000) {
                    inputDom.next().val(result.uploadFileDTO.fileId);
                    inputDom.parent().parent().next().html(fileName);
                    inputDom.parent().parent().next().next().css("display","inline-block");
                }
            });
            //filenameMd5(SystemConfig.fileUploadUrl+ SystemConfig.appKey, "file2");

        } else {
            $(this).parent().parent().next()[0].innerHTML = "jpg/gif格式";
            $(this).next().val("");
        }
    });
    $(".delFile").on("click",function(){
        document.getElementById("fileForm").reset();
        $(this).prev()[0].innerHTML = "jpg/gif格式";
        $(this).parent().find("input[type=hidden]").val("");
        $(this).css("display","none");
    });
    //选择题型
    $('[name="select"]').click(function (e) {
        $(this).find('ul').toggle();
        var values = $(this).find('input').val();
        $.each($(this).find('ul li'), function (i, data) {
            if (data.innerHTML == values) {
                data.className = "on";
            } else {
                data.className = "";
            }
        });
        e.stopPropagation();
    });
    $('[name="select"] li').hover(function (e) {
        $.each($("[name='select'] ul li"), function (i, data) {
            data.className = "";
        });
        $(this).toggleClass('on');
        e.stopPropagation();
    });

    //选择题型
    $('[name="select"] li').click(function (e) {
        var val = $(this).text();
        var dataVal = $(this).attr("data-value");
        $(this).parents('[name="select"]').find('input').val(val);
        $(this).parents('[name="select"]').find('input').attr("data-value",dataVal)
        $('[name="select"] ul').hide();
        $.each($("#upload input"),function(i,data){
            data.value="";
            $(this).parent().next().html(" ")
        })
        if(val=="选择题"){
            $("#selected").css("display","inline-block");
            $("#selection").css("display","block");
            $("#selection .optionsNum").css("display","inline-block");
            $("#subjectFill").css("display","none");
            var html=
                '<span class="radioItem on">'+
                    '<input type="radio"  name="selectType"  checked="true" class="hiddenClass" id="radioSelect">'+
                '</span>'+
                '<label for="radioSelect">单选</label>'+
                '<span class="radioItem">'+
                    '<input type="radio"  name="selectType"  class="hiddenClass" id="multiSelect">'+
                '</span>'+
                '<label for="multiSelect">多选</label>';
                //'<span class="radioItem">'+
                //    '<input type="radio" name="selectType"  class="hiddenClass" id="judgSelect">'+
                //'</span>'+
                //'<label for="judgSelect">判断题</label>';
            $("#selected").html(html);
            $("#selection .optionsNum input").val(4);
            getRadioSelection(4);
        }else if(val=="主观填空"){
            $("#selected").css("display","none");
            $("#selection").css("display","none");
            $("#subjectFill").css("display","block");
            var html=
            '<span class="radioItem">'+
                '<input type="radio"  name="subjectFill"  value="2" class="hiddenClass" id="handWrite">'+
            '</span>'+
            '<label for="handWrite">手写</label>'+
            '<span class="radioItem">'+
                '<input type="radio"  name="subjectFill"  value="1" class="hiddenClass" id="textInput">'+
            '</span>'+
            '<label for="textInput">文本输入</label>'+
            '<span class="selectError">请选择输入方式</span>';
            $("#subjectFill .options").html(html);
        }else{
            $("#selected").css("display","none");
            $("#selection").css("display","none");
            $("#subjectFill").css("display","none");
        }
        e.stopPropagation();
    });
    $(document).click(function () {
        $('[name="select"] ul').hide();
    });

    //选择题详情
    getRadioSelection(4);
    $("#selected").on("change","input",function(){
        if($("#radioSelect")[0].checked){
            $("#selection .optionsNum").css("display","inline-block");
            $("#selection .optionsNum input").val(4)
            getRadioSelection(4);
        }else if($("#multiSelect")[0].checked){
            $("#selection .optionsNum").css("display","inline-block");
            $("#selection .optionsNum input").val(4)
            getMultiSelection(4);
        }else{
            $("#selection .optionsNum").css("display","none");
            var html=
            '<span>答案：</span>'+
            '<span class="radioItem">'+
                '<input type="radio" checked="checked" name="judgSelection"  class="hiddenClass" value="1" id="yes">'+
            '</span>'+
            '<label for="yes">正确</label>'+
            '<span class="radioItem">'+
                '<input type="radio" checked="checked" name="judgSelection"  class="hiddenClass" value="0" id="no">'+
            '</span>'+
            '<label for="no">错误</label>'+
            '<span class="selectError">请选择一个答案</span>';
            $("#selection .answer .options").html(html);
        }
    });
    //选项改变确定判断选项数
    $("#selection .optionsNum input")[0].onchange=function(){
        getOptionsNum($(this)[0]);
        var num=parseInt($(this).val());
        if($("#radioSelect")[0].checked){
            getRadioSelection(num);
        }else {
            getMultiSelection(num);
        }
    };
    //选项失去焦点判断选项数
    $("#selection .optionsNum input")[0].onblur=function(){
        getOptionsNum($(this)[0]);
        var num=parseInt($(this).val());
        if($("#radioSelect")[0].checked){
            getRadioSelection(num);
        }else {
            getMultiSelection(num);
        }
    };
    //点击+号
    $("#selection .optionsNum .plus").click(function(){
        var num = parseInt($('#selection .optionsNum input').val());
        num++;
        if(num>8) return;
        $('#selection .optionsNum input').val(num);
        if($("#radioSelect")[0].checked){
            getRadioSelection(num);
        }else {
            getMultiSelection(num);
        }
    });
    //点击-号
    $("#selection .optionsNum .reduce").click(function(){
        var num = parseInt($('#selection .optionsNum input').val());
        num--;
        if(num<2) return;
        $('#selection .optionsNum input').val(num);
        if($("#radioSelect")[0].checked){
            getRadioSelection(num);
        }else {
            getMultiSelection(num);
        }
    });
    //点击保存发送数据
    $("#save .saveTopic").click(function(){
        //将ueditor img src64 转换为_src中文件中的上传到地址服务器的地址
        $.each($("#myEditor img"),function(i,data){
            data.setAttribute("src",data.getAttribute("_src"));
        });
        var type=$("#questionType .select input").attr("data-value");
        if(($("#questionType .select input").attr("data-value")=="1")&&($("#radioSelect")[0].checked||$("#multiSelect")[0].checked)){
            var optionNum=$("#selection .optionsNum input").val();
            var answers=[];
            $.each($("#selection .answer .options input"),function(i,data){
                if(data.checked){
                    if(data.id=="yes"){
                        answers.push("Y");
                    }else if(data.id=="no"){
                        answers.push("N");
                    }else{
                        answers.push(data.id);
                    }
                }
            });
        }

        //if(($("#questionType .select input").attr("data-value")=="1")&&($("#judgSelect")[0].checked)){
        //    var verdict=$("#selection .answer .options input:checked").val();
        //}
        var verdict="";
        if($("#questionType .select input").attr("data-value")=="3"){
            var inputMode=$("#subjectFill .options input:checked").val()
        }
        if($("#upload .mp3 input:last-child").val()){
            var hearingFile=$("#upload .mp3 input:last-child").val();
        }
        if($("#upload .jpg input:last-child").val()){
            var answerFile=$("#upload .jpg input:last-child").val();
        }
        var content=UM.getEditor('myEditor').getContent();
        var dataValue=$(".select input").attr("data-value");
        if(content==""){
            if(dataValue==1){
                if(!$(".answer").find(".on").length&&!$(".answer").find(".on_check").length){
                    $(".selectError").css("display","inline-block");
                }else{
                    $(".selectError").css("display","none");
                }
            }else if(dataValue==3){
                if(!$("#subjectFill .options").find(".on").length){
                    $(".selectError").css("display","inline-block");
                }else{
                    $(".selectError").css("display","none");
                }
            }
            //$("#modelClick").trigger('click');
            layer.confirm('请添加内容', {
                btn: ['确定'] //按钮
            }, function(index, layero){
                layer.close(index);
            });
        }else{
            if(dataValue==1){
                if(!$(".answer").find(".on").length&&!$(".answer").find(".on_check").length){
                    $(".selectError").css("display","inline-block");
                }else{
                    $(".selectError").css("display","none");
                    if(sessionStorage.getItem("topicIdUpdate")==null){
                        var topicGroupIdTo = sessionStorage.getItem('topicGroupIdTo');
                        if(topicGroupIdTo==null){
                            courseTopicAdd(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,gotopicList);
                        }else{
                            courseTopicGrouAdd(type,topicGroupIdTo,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,goTopicManageDetail)
                        }
                    }else{
                        var topicId=sessionStorage.getItem("topicIdUpdate");
                        topicIdUpdate(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,topicId);
                        sessionStorage.removeItem("topicIdUpdate");
                    }
                }
            }else if(dataValue==3){
                if(!$("#subjectFill .options").find(".on").length){
                    $(".selectError").css("display","inline-block");
                }else{
                    $(".selectError").css("display","none");
                    if(sessionStorage.getItem("topicIdUpdate")==null){
                        var topicGroupIdTo = sessionStorage.getItem('topicGroupIdTo');
                        if(topicGroupIdTo==null){
                            courseTopicAdd(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,gotopicList);
                        }else{
                            courseTopicGrouAdd(type,topicGroupIdTo,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,goTopicManageDetail)
                        }
                    }else{
                        var topicId=sessionStorage.getItem("topicIdUpdate");
                        topicIdUpdate(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,topicId);
                        sessionStorage.removeItem("topicIdUpdate")
                    }
                }
            }else if(dataValue==5){//如果是阅读题点保存
                if(sessionStorage.getItem("topicIdUpdate")==null){
                    var topicGroupIdTo = sessionStorage.getItem('topicGroupIdTo');
                    if(topicGroupIdTo==null){
                        courseTopicAdd(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,function(data){
                            data = JSON.parse(data);
                            sessionStorage.setItem('topicReadingId',data.id);
                            openUrl(window.location.href,"topicList.html","题目列表");
                            openUrl(window.location.href,"AddReadingTopic.html","添加小题");
                            // window.top.xhTop.closeTabByName("添加题目");
                        });
                    }else{
                        courseTopicGrouAdd(type,topicGroupIdTo,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,goTopicManageDetail)
                    }
                }else{
                    var topicId=sessionStorage.getItem("topicIdUpdate");
                    topicIdUpdate(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,topicId);
                    sessionStorage.removeItem("topicIdUpdate")
                }
            }else{
                if(sessionStorage.getItem("topicIdUpdate")==null){
                    var topicGroupIdTo = sessionStorage.getItem('topicGroupIdTo');
                    if(topicGroupIdTo==null){
                        courseTopicAdd(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,gotopicList);
                    }else{
                        courseTopicGrouAdd(type,topicGroupIdTo,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,goTopicManageDetail)
                    }
                }else{
                    var topicId=sessionStorage.getItem("topicIdUpdate");
                    topicIdUpdate(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,topicId);
                    sessionStorage.removeItem("topicIdUpdate")
                }
            }
        }
    });
    function topicIdUpdate(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,topicId){
        var topicIdUpdate = {
            "type": type,
            "optionNum": optionNum==undefined?"":optionNum,
            "answers": answers,
            "verdict": verdict==undefined?"":verdict,
            "inputMode": inputMode==undefined?"":inputMode,
            "answerFile": answerFile==undefined?"":answerFile,
            "hearingFile": hearingFile==undefined?"":hearingFile,
            "content": content,
            "topicId":topicId,
            "token":User.token,
            "timeStamp": 11111,
            "os": "qqq",
            "version":"1",
            "session":"1",
            "signature":"1"
        };
        topicIdUpdate = JSON.stringify(topicIdUpdate);
        $.ajax({
            type: 'POST',  //POST/PUT/DELETE/HEAD
            url: Doman.url+Doman.topicUpdate,
            contentType: "application/json",
            data: topicIdUpdate,
            success: function (data) {
                //对面包屑进行编辑
                $(parent.document).find(".layui-breadcrumb a:last-child").remove();
                $(parent.document).find(".layui-breadcrumb a:last-child .layui-box").remove();

                var topicReadingId=sessionStorage.getItem("topicReadingId");
                if(topicReadingId!=null){
                    var topicDetailNameModify=sessionStorage.getItem("topicDetailNameModify");
                    sessionStorage.removeItem("topicDetailNameModify");
                    openUrl(window.location.href,"topicList.html","题目列表");
                    openUrl(window.location.href,"TopicManageDetail.html",topicDetailNameModify);
                    openUrl(window.location.href,"AddReadingTopic.html","添加小题");
                    // window.top.xhTop.closeTabByName("修改题目");
                }else{
                    var topicDetailNameModify=sessionStorage.getItem("topicDetailNameModify");
                    sessionStorage.removeItem("topicDetailNameModify");
                    openUrl(window.location.href,"topicList.html","题目列表");
                    openUrl(window.location.href,"TopicManageDetail.html",topicDetailNameModify);
                    // window.top.xhTop.closeTabByName("修改题目");
                }
            },	//成功响应的回调
            error: function () {
                console.log("error");
            }		//失败响应的回调
        });
    }
    function courseTopicAdd(type,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,callback){
        var courseTopicAdd = {
            "type": type,
            "optionNum": optionNum==undefined?"":optionNum,
            "answers": answers,
            "verdict": verdict==undefined?"":verdict,
            "inputMode": inputMode==undefined?"":inputMode,
            "answerFile": answerFile==undefined?"":answerFile,
            "hearingFile": hearingFile==undefined?"":hearingFile,
            "content": content,
            "token":User.token,
            "timeStamp": 11111,
            "os": "qqq",
            "version":"1",
            "session":"1",
            "signature":"1"
        };
        courseTopicAdd = JSON.stringify(courseTopicAdd);
        $.ajax({
            type: 'POST',  //POST/PUT/DELETE/HEAD
            url: Doman.url+Doman.topicSave,
            contentType: "application/json",
            data: courseTopicAdd,
            success: function (data) {
                callback(data);
            },	//成功响应的回调
            error: function () {
                console.log("error");
            }		//失败响应的回调
        });
    }
    function courseTopicGrouAdd(type,topicGroupIdTo,optionNum,answers,verdict,inputMode,answerFile,hearingFile,content,callback){
        //var topicGroupIdTo = sessionStorage.getItem('topicGroupIdTo');
        var courseTopicGrouAdd = {
            "type": type,
            "topicGroupId" : topicGroupIdTo,
            "optionNum": optionNum==undefined?"":optionNum,
            "answers": answers,
            "verdict": verdict==undefined?"":verdict,
            "inputMode": inputMode==undefined?"":inputMode,
            "answerFile": answerFile==undefined?"":answerFile,
            "hearingFile": hearingFile==undefined?"":hearingFile,
            "content": content,
            "token":User.token,
            "timeStamp": 11111,
            "os": "qqq",
            "version":"1",
            "session":"1",
            "signature":"1"
        };
        courseTopicGrouAdd = JSON.stringify(courseTopicGrouAdd);
        $.ajax({
            type: 'POST',  //POST/PUT/DELETE/HEAD
            url: Doman.url+Doman.topicGroupSave,
            contentType: "application/json",
            data: courseTopicGrouAdd,
            success: function (data) {
                callback(data);
            },	//成功响应的回调
            error: function () {
                console.log("error");
            }		//失败响应的回调
        });
    }
    function gotopicList(data){
        //对面包屑进行编辑
        $(parent.document).find(".layui-breadcrumb a:last-child").remove();
        $(parent.document).find(".layui-breadcrumb a:last-child .layui-box").remove();

        openUrl(window.location.href,"topicList.html","题目列表");
        // window.top.xhTop.closeTabByName("添加题目");
    }
    function goTopicManageDetail(data){
        //对面包屑进行编辑
        $(parent.document).find(".layui-breadcrumb a:last-child").remove();
        $(parent.document).find(".layui-breadcrumb a:last-child .layui-box").remove();

        data = JSON.parse(data);
        var topicDetailName=sessionStorage.getItem("topicDetailName");
        var arr=topicDetailName.split("-");
        var topicDetaiNextlName="1-"+(parseInt(arr[1])+1)+"题";
        var topicGroupIdName=sessionStorage.getItem("topicGroupIdName");
        sessionStorage.removeItem('topicGroupIdTo');
        sessionStorage.removeItem('topicGroupIdName');
        sessionStorage.setItem('topicDetailName',topicDetaiNextlName);
        sessionStorage.setItem('topicGroupId',data.id);
        // window.top.xhTop.closeTabByName(topicDetailName);
        openUrl(window.location.href,"topicList.html","题目列表");
        openUrl(window.location.href,"TopicManageDetail.html",topicDetaiNextlName);
        // window.top.xhTop.closeTabByName(topicGroupIdName);
    }

    //实例化编辑器
    var serverPath = '/';
    var um = UM.getEditor('myEditor', {
        imageUrl: "http://files.yunzuoye.net/XHFileServer/file/upload/"+SystemConfig.appKey+"/"+SystemConfig.fileMd5,
        //imageUrl: "http://192.168.0.180/XHFileServer/file/upload/"+SystemConfig.appKey+"/"+SystemConfig.fileMd5,
        imagePath: serverPath,
        lang: /^zh/.test(navigator.language || navigator.browserLanguage || navigator.userLanguage) ? 'zh-cn' : 'en',
        langPath: UMEDITOR_CONFIG.UMEDITOR_HOME_URL + "lang/",
        focus: true,
        toolbar: [
            'source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor ',
            '| justifyleft justifycenter justifyright justifyjustify |',
            '| upload_pic ',
            '| custom_w1 custom_w2 custom_w3 custom_w4 custom_w5 custom_w6 custom_w7 custom_w8 custom_w9 custom_w10 custom_w11',
            '| custom_p1 custom_p2 custom_p3 custom_p4 custom_p5 custom_p6 custom_p7 custom_p8 custom_p9 custom_p10 custom_p11',
            '| custom_a1 custom_a2 custom_a3 custom_a4 custom_o1 custom_o2 custom_o3 custom_o4 custom_e1 custom_e2 custom_e3 custom_e4',
            '| custom_i1 custom_i2 custom_i3 custom_i4 custom_u1 custom_u2 custom_u3 custom_u4 custom_yu1 custom_yu2 custom_yu3 custom_yu4',
            '| custom_s1 custom_s2 custom_s3 custom_s4 custom_s5 custom_s6 custom_s7 custom_s8 custom_s9 custom_s10 custom_s11 custom_s12 custom_s13 custom_s14 custom_s15 custom_s16',
            '| formula1 formula2 formula3 formula4 formula5 formula6 formula7 formula8 formula9 formula ',
            '| answer explain '
        ]
    });

    $("#imageUp").on("click",function(){
        um.execCommand( 'insertimage', {
            src:'http://avatar.csdn.net/8/2/6/1_wangwangpiao.jpg'
        } );
    });

    um.addListener('blur', function () {
        $('#focush2').html('编辑器失去焦点了')
    });
    um.addListener('focus', function () {
        $('#focush2').html('')
    });
    /*如果存在topicId 则为修改页面*/
    var topicId=sessionStorage.getItem("topicId");
    sessionStorage.removeItem("topicId");
    if(topicId!=null){
        var topicIdData = {
            "topicId": topicId,
            "token":User.token
        };
        topicIdData = JSON.stringify(topicIdData);
        $.ajax({
            type: 'POST',  //POST/PUT/DELETE/HEAD
            url: Doman.url+Doman.topicIdGet,
            contentType: "application/json",
            data: topicIdData,
            success: function (data) {
                data = JSON.parse(data).topicDetail;
                addTopicDetail(data);
                sessionStorage.setItem("topicIdUpdate",data.topicId);
            },	//成功响应的回调
            error: function () {
                console.log("error");
            }		//失败响应的回调
        });
    }
});


//实例化编辑器对象
function   uploadPic(){
    var picUrl="";
    //TOD 调用文件选择框
    var fInput=document.getElementById("uploadPic");
    fInput.click();
    //TOD 上传文件到文件服务器
    var flag=true;
    $("#uploadPic").on('change', function(){
        layer.load(1);
        if(flag){
            flag=false;
            if(!$(this)[0].value==""){
                // filenameMd5x(SystemConfig.fileUploadUrl+ SystemConfig.appKey,"uploadPic");
                console.log(SystemConfig.fileUploadUrl+ SystemConfig.appKey)
                uploadFile(SystemConfig.fileUploadUrl+ SystemConfig.appKey + "/",$(this).get(0).files[0],function(result){
                    var html = "";
                    var result = JSON.parse(result);
                    if (result.status == 1000) {

                        innerPic(result.uploadFileDTO.fileId);
                    }
                    layer.closeAll('loading');
                });
            }
        }
        flag=false;
    });
}

function innerPic(url){
    //TOD 将文件服务器返回图片地址插入到编辑器
    UM.getEditor('myEditor').execCommand( 'insertimage', {
        src:url,
        _src:url
        //width: 300,
        //height:400
    } );
}



//按钮的操作
function insertHtml() {
    var value = prompt('插入html代码', '');
    um.execCommand('insertHtml', value)
}

function isFocus() {
    alert(um.isFocus())
}

function doBlur() {
    um.blur()
}

function createEditor() {
    enableBtn();
    um = UM.getEditor('myEditor');
}

function getAllHtml() {
    alert(UM.getEditor('myEditor').getAllHtml())
}

function getContent() {
    var arr = [];
    arr.push("使用editor.getContent()方法可以获得编辑器的内容");
    arr.push("内容为：");
    arr.push(UM.getEditor('myEditor').getContent());
    alert(arr.join("\n"));
}

function getPlainTxt() {
    var arr = [];
    arr.push("使用editor.getPlainTxt()方法可以获得编辑器的带格式的纯文本内容");
    arr.push("内容为：");
    arr.push(UM.getEditor('myEditor').getPlainTxt());
    alert(arr.join('\n'))
}

function setContent(isAppendTo) {
    var arr = [];
    arr.push("使用editor.setContent('欢迎使用umeditor')方法可以设置编辑器的内容");
    UM.getEditor('myEditor').setContent('欢迎使用umeditor', isAppendTo);
    alert(arr.join("\n"));
}

function setDisabled() {
    UM.getEditor('myEditor').setDisabled('fullscreen');
    disableBtn("enable");
}

function setEnabled() {
    UM.getEditor('myEditor').setEnabled();
    enableBtn();
}

function getText() {
    //当你点击按钮时编辑区域已经失去了焦点，如果直接用getText将不会得到内容，所以要在选回来，然后取得内容
    var range = UM.getEditor('myEditor').selection.getRange();
    range.select();
    var txt = UM.getEditor('myEditor').selection.getText();
    alert(txt)
}

function getContentTxt() {
    var arr = [];
    arr.push("使用editor.getContentTxt()方法可以获得编辑器的纯文本内容");
    arr.push("编辑器的纯文本内容为：");
    arr.push(UM.getEditor('myEditor').getContentTxt());
    alert(arr.join("\n"));
}

function hasContent() {
    var arr = [];
    arr.push("使用editor.hasContents()方法判断编辑器里是否有内容");
    arr.push("判断结果为：");
    arr.push(UM.getEditor('myEditor').hasContents());
    alert(arr.join("\n"));
}

function setFocus() {
    UM.getEditor('myEditor').focus();
}

function deleteEditor() {
    disableBtn();
    UM.getEditor('myEditor').destroy();
}

function disableBtn(str) {
    var div = document.getElementById('btns');
    var btns = domUtils.getElementsByTagName(div, "button");
    for (var i = 0, btn; btn = btns[i++];) {
        if (btn.id == str) {
            domUtils.removeAttributes(btn, ["disabled"]);
        } else {
            btn.setAttribute("disabled", "true");
        }
    }
}

function enableBtn() {
    var div = document.getElementById('btns');
    var btns = domUtils.getElementsByTagName(div, "button");
    for (var i = 0, btn; btn = btns[i++];) {
        domUtils.removeAttributes(btn, ["disabled"]);
    }
}


function getSelectedText() {
    if (window.getSelection) {
        // This technique is the most likely to be standardized.
        // getSelection() returns a Selection object, which we do not document.
        return window.getSelection().toString();
    } else if (document.getSelection) {
        // This is an older, simpler technique that returns a string
        return document.getSelection();
    }else if (document.selection) { // This is the IE-specific technique. // We do not document the IE selection property or TextRange objects.
        return document.selection.createRange().text;
    }
}
//判断是否input超过阈值
function getOptionsNum(_input){
    if(_input.validity.rangeOverflow){
        _input.value=8;
    }else if(_input.validity.rangeUnderflow){
        _input.value=2;
    }
}
//获取单选html
function getRadioSelection(num){
    var arr=["A","B","C","D","E","F","G","H"];
    var html='';
    for(var i=0;i<num;i++){
        html+=
        '<span class="radioItem">'+
            '<input type="radio"  name="radioSelection"  class="hiddenClass" id='+arr[i]+'>'+
        '</span>'+
        '<label for='+arr[i]+'>'+arr[i]+'</label>';
    }
    html+='<span class="selectError">请选择一个答案</span>';
    $("#selection .answer .options").html(html);
}
//获取多选html
function getMultiSelection(num){
    var arr=["A","B","C","D","E","F","G","H"];
    var html='';
    for(var i=0;i<num;i++){
        html+=
        '<span class="multiItem">'+
            '<input name="multiSelection" type="checkbox"  class="hiddenClass"  id='+arr[i]+'>'+
        '</span>'+
        '<label for='+arr[i]+'>'+arr[i]+'</label>';
    }
    html+='<span class="selectError">请选择多个答案</span>';
    $("#selection .answer .options").html(html);
}

//单选
$("#selection .answer .options").on("click",".radioItem input",function(){
    $(this).parent().addClass("on").siblings().removeClass("on");
});
$("#selected").on("click",".radioItem input",function(){
    $(this).parent().addClass("on").siblings().removeClass("on");
});
$("#subjectFill").on("click",".radioItem input",function(){
    $(this).parent().addClass("on").siblings().removeClass("on");
});
//多选
$("#selection .answer .options").on("click",".multiItem input",function(){
    $(this).parent().toggleClass( "on_check" );
});
function addTopicDetail(data){
    var select=$("#questionType .select");
    var radioItem=$("#selected .radioItem");
    var options=$("#selection .answer .options");
    var type=data.topicGetData.type;
    if(type==9){//如果是判断题
        radioItem[2].setAttribute("class","radioItem on");
        radioItem[1].setAttribute("class","radioItem");
        radioItem[0].setAttribute("class","radioItem");
        $("#selected .on input")[0].checked="true";
        $("#selection .optionsNum").css("display","none");
        var html=
            '<span>答案：</span>'+
            '<span class="radioItem">'+
                '<input type="radio" checked="checked" name="judgSelection"  class="hiddenClass" value="1" id="yes">'+
            '</span>'+
            '<label for="yes">正确</label>'+
            '<span class="radioItem">'+
                '<input type="radio" checked="checked" name="judgSelection"  class="hiddenClass" value="0" id="no">'+
            '</span>'+
            '<label for="no">错误</label>'+
            '<span class="selectError">请选择一个答案</span>';
        options.html(html);
        var answer=data.topicGetData.answers;
        if(answer=="Y"){
            options.find(".radioItem")[0].setAttribute("class","radioItem on");
            options.find(".radioItem")[1].setAttribute("class","radioItem");
        }else{
            options.find(".radioItem")[1].setAttribute("class","radioItem on");
            options.find(".radioItem")[0].setAttribute("class","radioItem");
        }
    }else if(type==1){//如果是单选多选题
        var arr=data.topicGetData.answers.split(", ");
        $("#selection .optionsNum input").val(data.topicGetData.optionNum);
        if(arr.length==1){//单选
            $("#radioSelect")[0].checked="true";
            radioItem[0].setAttribute("class","radioItem on");
            radioItem[1].setAttribute("class","radioItem");
            getRadioSelection(data.topicGetData.optionNum);
            $("#"+arr[0]).parent().addClass("on");
            $("#"+arr[0])[0].checked="true";
        }else{//多选
            $("#multiSelect")[0].checked="true";
            radioItem[0].setAttribute("class","radioItem");
            radioItem[1].setAttribute("class","radioItem on");
            getMultiSelection(data.topicGetData.optionNum);
            for(var i=0;i<arr.length;i++){
                $("#"+arr[i]).parent().addClass("on_check");
                $("#"+arr[i])[0].checked="true";
            }
        }
        $("#selected .on input")[0].checked="true";
    }else if(type==3){//如果是主观题
        $("#selected").css("display","none");
        $("#selection").css("display","none");
        $("#subjectFill").css("display","block");
        var html=
            '<span class="radioItem">'+
                '<input type="radio"  name="subjectFill"  value="2" class="hiddenClass" id="handWrite">'+
            '</span>'+
            '<label for="handWrite">手写</label>'+
            '<span class="radioItem">'+
                '<input type="radio"  name="subjectFill"  value="1" class="hiddenClass" id="textInput">'+
            '</span>'+
            '<label for="textInput">文本输入</label>'+
            '<span class="selectError">请选择输入方式</span>';
        $("#subjectFill .options").html(html);
        var radioItemNode=$("#subjectFill .options .radioItem");
        if(data.topicGetData.inputMode==1){
            radioItemNode[0].setAttribute("class","radioItem");
            radioItemNode[1].setAttribute("class","radioItem on");
        }else{
            radioItemNode[1].setAttribute("class","radioItem");
            radioItemNode[0].setAttribute("class","radioItem on");
        }
        $("#subjectFill .on input")[0].checked="true";
        select.find("input").attr("data-value",type);
        select.find("ul li")[type].setAttribute("class","on");
        select.find("input").val(select.find("ul li")[type].innerHTML);
    }else{
        $("#selected").css("display","none");
        $("#selection").css("display","none");
        select.find("input").attr("data-value",type);
        select.find("ul li")[type].setAttribute("class","on");
        select.find("input").val(select.find("ul li")[type].innerHTML);
    }
    if(data.topicGetData.workPicFile){
        var jpg_result= $("#jpg_result");
        jpg_result.val(data.topicGetData.workPicFile);
        jpg_result.parent().parent().next().html(data.topicGetData.workPicFile);
        jpg_result.parent().parent().next().next().css("display","inline-block");
    }
    $("#myEditor").html(data.topicGetData.content);
};


